import math
import numpy as np
from conf.app_config import AppConfig as AC

class RadarEquation(object):
    def __init__(self):
        self.name = 'irlab.basics.radar_equation.RadarEquation'

    @staticmethod
    def radar_equation(pt, freq, g, sigma, te, b, nf, loss, range_):
        '''
        其他雷达方程的实现
        '''
        # Constants
        c = 3.0e+8  # speed of light
        lambda_ = AC.C / freq  # wavelength
        p_peak = 10 * np.log10(pt)  # convert peak power to dB
        lambda_sqdb = 10 * np.log10(lambda_**2)  # compute wavelength square in dB
        sigmadb = 10 * np.log10(sigma)  # convert sigma to dB
        four_pi_cub = 10 * np.log10((4.0 * np.pi)**3)  # (4pi)^3 in dB
        k_db = 10 * np.log10(AC.k)  # Boltzman’s constant in dB
        te_db = 10 * np.log10(te)  # noise temp. in dB
        b_db = 10 * np.log10(b)  # bandwidth in dB
        range_pwr4_db = 10 * np.log10(range_**4)  # vector of target range^4 in dB
        nf_db = 10*np.log10(nf)
        loss_db = 10*np.log10(loss)
        # Implement Equation (1.56)
        g_sqdb = 10*np.log10(g**2)
        num = p_peak + g_sqdb + lambda_sqdb + sigmadb
        den = four_pi_cub + k_db + te_db + b_db + nf_db + loss_db + range_pwr4_db
        snr = num - den

        return snr
    
    @staticmethod
    def radar_equation_R_refx(pt, freq, g, sigma, te, b, nf, loss, snr):
        '''
        其他雷达方程的实现
        '''
        # Constants
        c = 3.0e+8  # speed of light
        lambda_ = c / freq  # wavelength
        p_peak = 10 * np.log10(pt)  # convert peak power to dB
        lambda_sqdb = 10 * np.log10(lambda_**2)  # compute wavelength square in dB
        sigmadb = 10 * np.log10(sigma)  # convert sigma to dB
        four_pi_cub = 10 * np.log10((4.0 * np.pi)**3)  # (4pi)^3 in dB
        k_db = 10 * np.log10(1.38e-23)  # Boltzman’s constant in dB
        te_db = 10 * np.log10(te)  # noise temp. in dB
        b_db = 10 * np.log10(b)  # bandwidth in dB
        # range_pwr4_db = 10 * np.log10(range_**4)  # vector of target range^4 in dB

        # Implement Equation (1.56)
        num = p_peak + g + g + lambda_sqdb + sigmadb
        range_pwr4_db = num - four_pi_cub - k_db - te_db - b_db - nf - loss - snr
        range_pwr4 = 10 ** (range_pwr4_db / 10.0)
        range_ = range_pwr4 ** 0.25

        return range_
    
    @staticmethod
    def radar_equation_R_ref(pt, freq, g, sigma, te, b, nf, loss, snr, tau):
        '''
        其他雷达方程的实现
        '''
        # Constants
        c = 3.0e+8  # speed of light
        lambda_ = c / freq  # wavelength
        p_peak = 10 * np.log10(pt)  # convert peak power to dB
        lambda_sqdb = 10 * np.log10(lambda_**2)  # compute wavelength square in dB
        sigmadb = 10 * np.log10(sigma)  # convert sigma to dB
        four_pi_cub = 10 * np.log10((4.0 * np.pi)**3)  # (4pi)^3 in dB
        k_db = 10 * np.log10(1.38e-23)  # Boltzman’s constant in dB
        te_db = 10 * np.log10(te)  # noise temp. in dB
        b_db = 10 * np.log10(b)  # bandwidth in dB
        tau_db = 10 * np.log10(tau)
        # range_pwr4_db = 10 * np.log10(range_**4)  # vector of target range^4 in dB

        # Implement Equation (1.56)
        num = p_peak + g + g + lambda_sqdb + sigmadb + tau_db
        range_pwr4_db = num - four_pi_cub - k_db - te_db - nf - loss - snr
        range_pwr4 = 10 ** (range_pwr4_db / 10.0)
        range_ = range_pwr4 ** 0.25

        return range_
    
    @staticmethod
    def R_ref_to_R():
        R = 0.0
        return R
